<template>
  <div>
    <el-row type="flex" justify="end" class="button-save">
      <el-button type="primary" @click="save">保存</el-button>
      <!-- <el-button type="primary" @click="test">test</el-button> -->
    </el-row>
    <el-form
      ref="bodyForm"
      :model="bodyInfo"
      :rules="bodyRules"
      :class="required == '1' ? 'require' : 'notRequire'"
      class="demo-form-inline pageForm"
      label-position="right"
      label-width="120px"
      size="small"
    >
      <el-form-item label="意识" prop="consciousness">
        <check-group
          v-model="bodyInfo.consciousness"
          :list="consciousnessList"
        ></check-group>
      </el-form-item>
      <el-form-item label="体位" prop="position">
        <radio-group
          v-model="bodyInfo.position"
          :list="positionList"
        ></radio-group>
      </el-form-item>
      <el-form-item label="皮肤颜色" prop="skincolor">
        <radio-group
          v-model="bodyInfo.skincolor"
          :list="skincolorList"
        ></radio-group>
      </el-form-item>
      <el-form-item label="头部" prop="head">
        <el-row class="head">
          <div class="pupil">
            <el-input
              v-model="bodyInfo.leftPupil"
              placeholder="左瞳孔"
              @input="keepDecimal('leftPupil')"
            >
              <template slot="append">mm</template>
            </el-input>
          </div>
          <div class="pupil">
            <el-input
              v-model="bodyInfo.rightPupil"
              placeholder="右瞳孔"
              @input="keepDecimal('rightPupil')"
            >
              <template slot="append">mm</template>
            </el-input>
          </div>
          <el-radio-group v-model="bodyInfo.sclera">
            <el-radio label="0">无巩膜黄染</el-radio>
            <el-radio label="1">有巩膜黄染</el-radio>
          </el-radio-group>
          <el-radio-group v-model="bodyInfo.reflection">
            <el-radio label="0">对光反射存在</el-radio>
            <el-radio label="1">对光反射消失</el-radio>
          </el-radio-group>
        </el-row>
        <el-row class="head">
          <el-radio-group v-model="bodyInfo.lips">
            <el-radio label="1">有口唇紫绀</el-radio>
            <el-radio label="0">无口唇紫绀</el-radio>
          </el-radio-group>
          <radio-group
            v-model="bodyInfo.tongue"
            :list="tongueList"
          ></radio-group>
          <el-radio-group v-model="bodyInfo.headOther">
            <el-radio label="0">其他</el-radio>
          </el-radio-group>
        </el-row>
      </el-form-item>
      <el-form-item label="颈部" prop="neck">
        <el-col :lg="4" :span="3">
          <el-radio-group v-model="bodyInfo.resistance">
            <el-radio label="0">无颈抵抗</el-radio>
            <el-radio label="1">有颈抵抗</el-radio>
          </el-radio-group>
        </el-col>
        <el-col :lg="5" :span="3" :offset="1">
          <el-radio-group v-model="bodyInfo.vein">
            <el-radio label="0">无静脉怒张</el-radio>
            <el-radio label="1">有静脉怒张</el-radio>
          </el-radio-group>
        </el-col>
        <el-col :lg="5" :span="3" :offset="1">
          <el-radio-group v-model="bodyInfo.svps">
            <el-radio label="0">无静脉搏动</el-radio>
            <el-radio label="1">有静脉搏动</el-radio>
          </el-radio-group>
        </el-col>
      </el-form-item>
      <el-form-item label="胸部" prop="chest">
        <el-col :lg="3" :xl="2">
          <el-radio-group v-model="bodyInfo.chestCheck">
            <el-radio label="0">拒查</el-radio>
            <el-radio label="1">未查</el-radio>
          </el-radio-group>
        </el-col>
        <el-col :span="2" :offset="1">
          <el-radio v-model="bodyInfo.deformity" label="0">
            对称无畸形
          </el-radio>
        </el-col>
        <el-col :lg="7" :xl="5" :offset="1">
          <el-radio-group v-model="bodyInfo.extrusion">
            <el-radio label="0">阴性胸廓挤压试验</el-radio>
            <el-radio label="1">阳性胸廓挤压试验</el-radio>
          </el-radio-group>
        </el-col>
        <el-col :span="1" :offset="1">
          <el-radio v-model="bodyInfo.deformity" label="1">其他</el-radio>
        </el-col>
      </el-form-item>
      <el-form-item label="呼吸" prop="breathing">
        <el-col :span="8">
          <el-radio-group v-model="bodyInfo.breathing">
            <el-radio label="0">停止</el-radio>
            <el-radio label="1">正常</el-radio>
          </el-radio-group>
        </el-col>
      </el-form-item>
      <el-form-item label="左肺" prop="leftLung">
        <el-col :span="24">
          <radio-group
            v-model="bodyInfo.leftLung"
            :list="LungList"
          ></radio-group>
        </el-col>
      </el-form-item>
      <el-form-item label="右肺" prop="rightLung">
        <el-col :span="24">
          <radio-group
            v-model="bodyInfo.rightLung"
            :list="LungList"
          ></radio-group>
        </el-col>
      </el-form-item>
      <el-form-item label="心脏" prop="heart">
        <el-row class="heart">
          <radio-group
            v-model="bodyInfo.heartSounds"
            :list="heartSounds"
          ></radio-group>
          <radio-group
            v-model="bodyInfo.rhythm"
            :list="rhythmList"
          ></radio-group>
        </el-row>

        <el-radio-group v-model="bodyInfo.noise">
          <el-radio label="0">无杂音</el-radio>
          <el-radio label="1">有杂音</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="腹部" prop="abdomenBelly">
        <el-row class="belly">
          <el-radio-group v-model="bodyInfo.abdomen">
            <el-radio label="0">腹部软</el-radio>
            <el-radio label="1">腹部肌紧张</el-radio>
          </el-radio-group>
          <el-radio-group v-model="bodyInfo.tenderness">
            <el-radio label="1">腹部有压痛</el-radio>
            <el-radio label="0">腹部无压痛</el-radio>
          </el-radio-group>
          <el-radio-group v-model="bodyInfo.reboundPain">
            <el-radio label="1">腹部有反跳痛</el-radio>
            <el-radio label="0">腹部无反跳痛</el-radio>
          </el-radio-group>
        </el-row>
        <el-row class="belly">
          <el-radio-group v-model="bodyInfo.liver">
            <el-radio label="0">肝脏未及</el-radio>
            <el-radio label="1">肝脏可及</el-radio>
          </el-radio-group>
          <el-radio-group v-model="bodyInfo.spleen">
            <el-radio label="0">脾脏未及</el-radio>
            <el-radio label="1">脾脏可及</el-radio>
          </el-radio-group>
          <radio-group
            v-model="bodyInfo.bowelSounds"
            :list="bowelSoundsList"
          ></radio-group>
        </el-row>
      </el-form-item>
      <el-form-item label="左上肢" prop="leftUpperLimb">
        <el-col :span="24">
          <radio-group
            v-model="bodyInfo.leftUpperLimb"
            :list="limbList"
          ></radio-group>
        </el-col>
      </el-form-item>
      <el-form-item label="左下肢" prop="leftLowerLimb">
        <el-col :span="24">
          <radio-group
            v-model="bodyInfo.leftLowerLimb"
            :list="limbList"
          ></radio-group>
        </el-col>
      </el-form-item>
      <el-form-item label="右上肢" prop="rightUpperLimb">
        <el-col :span="24">
          <radio-group
            v-model="bodyInfo.rightUpperLimb"
            :list="limbList"
          ></radio-group>
        </el-col>
      </el-form-item>
      <el-form-item label="右下肢" prop="rightLowerLimb">
        <el-col :span="24">
          <radio-group
            v-model="bodyInfo.rightLowerLimb"
            :list="limbList"
          ></radio-group>
        </el-col>
      </el-form-item>
      <el-form-item label="呼吸系统" prop="respiratorySystem">
        <check-group
          v-model="bodyInfo.respiratorySystem"
          :list="respiratorySystemList"
        ></check-group>
      </el-form-item>
      <el-form-item label="循环系统" prop="circulatorySystem">
        <check-group
          v-model="bodyInfo.circulatorySystem"
          :list="circulatorySystemList"
        ></check-group>
      </el-form-item>
      <el-form-item label="神经系统" prop="nervousSystem">
        <check-group
          v-model="bodyInfo.nervousSystem"
          :list="nervousSystemList"
        ></check-group>
      </el-form-item>
      <el-form-item label="骨骼系统" prop="skeletalSystem">
        <check-group
          v-model="bodyInfo.skeletalSystem"
          :list="skeletalSystemList"
        ></check-group>
      </el-form-item>
      <el-form-item label="消化系统" prop="digestiveSystem">
        <check-group
          v-model="bodyInfo.digestiveSystem"
          :list="digestiveSystemList"
        ></check-group>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import CheckGroup from './check-group'
import RadioGroup from './radio-group'
import {
  savePhysicalExamination,
  getPhysicalExamination
} from '@/api/triage/triage-new'
export default {
  name: 'TriagePhysique',
  components: {
    CheckGroup,
    RadioGroup
  },
  props: {
    id: {
      type: Object,
      default: () => {}
    },
    require: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      bodyInfo: {},
      fieldsRequired: {
        head: [
          'leftPupil',
          'rightPupil',
          'sclera',
          'reflection',
          'lips',
          'tongue',
          'headOther'
        ],
        neck: ['resistance', 'vein', 'svps'],
        chest: ['chestCheck', 'deformity', 'extrusion'],
        heart: ['heartSounds', 'rhythm', 'noise'],
        abdomenBelly: [
          'abdomen',
          'tenderness',
          'reboundPain',
          'liver',
          'spleen',
          'bowelSounds'
        ]
      },
      consciousnessList: [
        { name: '清楚', value: '清楚' },
        { name: '嗜睡', value: '嗜睡' },
        { name: '请妄', value: '请妄' },
        { name: '模糊', value: '模糊' },
        { name: '昏睡', value: '昏睡' },
        { name: '昏迷', value: '昏迷' },
        { name: '丧失', value: '丧失' }
      ],
      positionList: [
        { name: '平卧', value: '1' },
        { name: '半卧', value: '2' },
        { name: '端坐', value: '3' },
        { name: '自动', value: '4' },
        { name: '其他', value: '5' }
      ],
      skincolorList: [
        { name: '正常', value: '1' },
        { name: '苍白', value: '2' },
        { name: '发组', value: '3' },
        { name: '潮红', value: '4' },
        { name: '湿冷', value: '5' },
        { name: '黄染', value: '6' },
        { name: '有皮疹', value: '7' }
      ],
      tongueList: [
        { name: '伸舌居中', value: '1' },
        { name: '伸舌左偏', value: '2' },
        { name: '伸舌右偏', value: '3' }
      ],
      LungList: [
        { name: '干啰', value: '0' },
        { name: '湿罗音', value: '1' },
        { name: '呼吸音清', value: '2' },
        { name: '呼吸音粗糙', value: '3' },
        { name: '呼吸音减低', value: '4' },
        { name: '呼吸音消失', value: '5' }
      ],
      heartSounds: [
        { name: '心音正常', value: '0' },
        { name: '心音减低', value: '1' },
        { name: '心音强弱不等', value: '2' },
        { name: '心音消失', value: '3' }
      ],
      rhythmList: [
        { name: '心律齐', value: '0' },
        { name: '心律不齐', value: '1' },
        { name: '心律绝对不齐', value: '2' }
      ],
      bowelSoundsList: [
        { name: '肠鸣音正常', value: '0' },
        { name: '肠鸣音亢进', value: '1' },
        { name: '肠鸣音减弱', value: '2' },
        { name: '肠鸣音消失', value: '3' }
      ],
      limbList: [
        { name: '肌力正常', value: '0' },
        { name: '肌力减弱', value: '1' },
        { name: '有下肢水肿', value: '2' },
        { name: '肝脏无下肢水肿可及', value: '3' }
      ],
      respiratorySystemList: [
        { name: '正常', value: '正常' },
        { name: '咳嗽', value: '咳嗽' },
        { name: '咳痰', value: '咳痰' },
        { name: '呼吸困难', value: '呼吸困难' },
        { name: '胸痛及咳血', value: '胸痛及咳血' },
        { name: '发热', value: '发热' },
        { name: '盗汗', value: '盗汗' },
        { name: '结核病史', value: '结核病史' }
      ],
      circulatorySystemList: [
        { name: '正常', value: '正常' },
        { name: '胸闷', value: '胸闷' },
        { name: '心悸', value: '心悸' },
        { name: '气促', value: '气促' },
        { name: '发绀', value: '发绀' },
        { name: '心前区疼痛', value: '心前区疼痛' },
        { name: '头晕', value: '头晕' },
        { name: '头痛', value: '头痛' },
        { name: '昏厥', value: '昏厥' },
        { name: '少尿', value: '少尿' },
        { name: '下肢浮肿', value: '下肢浮肿' },
        { name: '高血压病史', value: '高血压病史' }
      ],
      nervousSystemList: [
        { name: '正常', value: '正常' },
        { name: '瘫痪', value: '瘫痪' },
        { name: '四肢抽搐', value: '四肢抽搐' },
        { name: '麻痹', value: '麻痹' },
        { name: '间歇性跛行', value: '间歇性跛行' },
        { name: '肢端疼痛', value: '肢端疼痛' },
        { name: '失眠', value: '失眠' },
        { name: '头痛', value: '头痛' },
        { name: '昏厥', value: '昏厥' },
        { name: '意识障碍', value: '意识障碍' },
        { name: '无记忆力', value: '无记忆力' },
        { name: '记忆力下降', value: '记忆力下降' },
        { name: '情绪异常', value: '情绪异常' },
        { name: '幻觉及定向力障碍', value: '幻觉及定向力障碍' }
      ],
      skeletalSystemList: [
        { name: '正常', value: '正常' },
        { name: '肌肉萎缩', value: '肌肉萎缩' },
        { name: '肢体麻木', value: '肢体麻木' },
        { name: '四肢无畸形', value: '四肢无畸形' },
        { name: '无辜摩擦感', value: '无辜摩擦感' },
        { name: '运动正常', value: '运动正常' },
        { name: '关节无红', value: '关节无红' }
      ],
      digestiveSystemList: [
        { name: '正常', value: '正常' },
        { name: '肌肉萎缩', value: '肌肉萎缩' },
        { name: '肢体麻木', value: '肢体麻木' },
        { name: '四肢无畸形', value: '四肢无畸形' },
        { name: '无辜摩擦感', value: '无辜摩擦感' },
        { name: '运动正常', value: '运动正常' },
        { name: '关节无红', value: '关节无红' },
        { name: '腹痛', value: '腹痛' },
        { name: '腹胀', value: '腹胀' },
        { name: '腹泻', value: '腹泻' },
        { name: '吞咽困难', value: '吞咽困难' },
        { name: '恶心呕吐', value: '恶心呕吐' },
        { name: '无呕血', value: '无呕血' },
        { name: '反酸嗳气', value: '反酸嗳气' },
        { name: '无便血', value: '无便血' },
        { name: '有便血', value: '有便血' },
        { name: '便秘', value: '便秘' },
        { name: '里急后重', value: '里急后重' }
      ],
      required: '',
      bodyRules: {
        consciousness: null,
        position: null,
        skincolor: null,
        head: null,
        neck: null,
        chest: null,
        breathing: null,
        leftLung: null,
        rightLung: null,
        heart: null,
        abdomenBelly: null,
        leftUpperLimb: null,
        leftLowerLimb: null,
        rightUpperLimb: null,
        rightLowerLimb: null,
        respiratorySystem: null,
        circulatorySystem: null,
        nervousSystem: null,
        skeletalSystem: null,
        digestiveSystem: null
      }
    }
  },
  created() {
    this.getPhysicalExamination()
    this.setRequire()
  },
  methods: {
    test() {
      this.$refs.bodyForm.clearValidate()
      for (let key in this.fieldsRequired) {
        let list = this.fieldsRequired[key]
        let i = 0
        for (i; i < list.length; i++) {
          let el = list[i]
          if (this.bodyInfo[el]) {
            this.$set(this.bodyInfo, key, '1')
            break
          }
        }
        if (i == list.length) {
          this.$set(this.bodyInfo, key, '')
        }
      }

      this.$nextTick(() => {
        this.$refs.bodyForm.validate((valid) => {
          if (!valid && this.required == '1') {
            this.$message.error('请通过验证')
          } else {
            this.$message.success('通过验证')
          }
        })
      })
    },
    keepDecimal(field) {
      this.bodyInfo[field] =
        (this.bodyInfo[field].match(/^\d+(\.?\d{0,2})/g) || '')[0] || ''
    },
    getPhysicalExamination() {
      getPhysicalExamination({ dispatchId: this.id.dispatchId }).then((res) => {
        if (res.code == 0 && res.data.length > 0) {
          this.bodyInfo = res.data[0]
        }
      })
    },
    setRequire() {
      if (this.require.length > 0) {
        this.required = this.require[0].value
        if (this.required !== '2') {
          Object.keys(this.bodyRules).forEach((el) => {
            this.bodyRules[el] = [
              {
                required: true,
                message: ' ',
                trigger: 'blur'
              }
            ]
          })
        } else {
          Object.keys(this.bodyRules).forEach((el) => {
            this.bodyRules[el] = null
          })
        }
      }
    },
    save() {
      this.$refs.bodyForm.clearValidate()
      for (let key in this.fieldsRequired) {
        let list = this.fieldsRequired[key]
        let i = 0
        for (i; i < list.length; i++) {
          let el = list[i]
          if (this.bodyInfo[el]) {
            this.$set(this.bodyInfo, key, '1')
            break
          }
        }
        if (i == list.length) {
          this.$set(this.bodyInfo, key, '')
        }
      }

      this.$nextTick(() => {
        this.$refs.bodyForm.validate((valid) => {
          if (!valid && this.required == '1') {
            this.$message.error('请通过验证')
          } else {
            this.bodyInfo.dispatchId = this.id.dispatchId
            this.bodyInfo.medicalId = this.id.medicalId
            this.bodyInfo.userId = this.$store.getters.userId
            savePhysicalExamination(this.bodyInfo).then((res) => {
              if (res.code == 0) {
                this.$message.success('保存成功')
                this.getPhysicalExamination()
              } else {
                this.$message.error('保存失败')
              }
            })
          }
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.head {
  .pupil {
    display: inline-block;
    width: 204px;
    margin-right: 30px;
  }
  .el-radio-group {
    margin-right: 90px;
  }
}
.heart {
  .el-radio-group {
    margin-right: 90px;
  }
}
.belly {
  .el-radio-group {
    margin-right: 90px;
  }
}
.button-save {
  padding-right: 40px;
  margin-bottom: 20px;
}
</style>
<style lang="scss">
.notRequire {
  .el-form-item.is-error .el-input__inner {
    border-color: #dcdfe6;
  }
}
</style>